package com.regentsoft.cloudback.operateAdmin.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.regentsoft.cloudback.base.Pager;
import com.regentsoft.cloudback.base.ResponseCode;
import com.regentsoft.cloudback.operateAdmin.dao.CompanyMapper;
import com.regentsoft.cloudback.operateAdmin.dao.PdaMachineCodeMapper;
import com.regentsoft.cloudback.operateAdmin.entity.Company;
import com.regentsoft.cloudback.operateAdmin.service.CompanyService;
import com.regentsoft.cloudback.vo.ActivationVo;
import com.regentsoft.cloudback.vo.CompanyDetailVo;
import com.regentsoft.cloudback.vo.resp.CompanyAddResp;

/**
 * 
 * @author czd
 *
 * @date 2017年11月25日
 *
 * @version 1.0
 */
@Service
public class CompanyServiceImpl implements CompanyService {
	@Autowired
	private CompanyMapper mapper;
	@Autowired
	private PdaMachineCodeMapper pdaMachineCodeMapper;
	@Override
	public Company findById(String pk) throws Exception {
		return mapper.findById(pk);
	}

	@Override
	public List<Company> findList(Company obj) throws Exception {
		return mapper.findList(obj);
	}

	@Override
	public Pager findPageList(Company obj, Integer pageNumber, Integer pageSize) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		obj.setSortColumns("create_time");
		params.put("model", obj);
		int total = getTotalCount(params);
		pageSize = pageSize == null || pageSize <= 0 ? 10 : pageSize;
		int totalPage = (total + pageSize - 1) / pageSize;
		pageNumber = pageNumber == null || pageNumber < 1 ? 1 : pageNumber > totalPage ? totalPage : pageNumber;
		Pager pager = new Pager(pageNumber, pageSize, total);
		if (total > 0) {
		    params.put("offset", (pager.getPageNumber() - 1) * pageSize);
		    params.put("pageSize", pageSize);
		    List<Company> list = this.queryByPage(params);
		    pager.setDatas(list);
		}
		return pager;
	}

	@Override
	public int add(Company obj) throws Exception {
		return mapper.add(obj);
	}

	@Override
	public int update(Company obj) throws Exception {
		return mapper.update(obj);
	}

	@Override
	public int delete(String pk) throws Exception {
		return mapper.deleteById(pk);
	}

	@Override
	public int getTotalCount(Map<String, Object> map) throws Exception {
		return mapper.getTotalCount(map);
	}

	@Override
	public List<Company> queryByPage(Map<String, Object> map) throws Exception {
		return mapper.queryByPage(map);
	}

	
	@Override
	@Transactional
	public CompanyAddResp initCompanyDB(Company company,CompanyAddResp resp) throws Exception {
		boolean unitFlag;
		//检查企业编码唯一性
		try {
			unitFlag = mapper.findByCode(company.getCode())==1;
			if(unitFlag) {
				resp.setCode(ResponseCode.FAIL);
				resp.setMessage("企业编码重复");
				return resp;
			}
			
			int i = mapper.add(company);
			if(i>0) {
				resp.setCode(ResponseCode.SUCCESS);
				mapper.initCompanyDB(company.getCode());
				mapper.createGetSerialNumberPro(company.getCode());
				/*mapper.initOrderSerialNumber(company.getCode());*/
				mapper.createKs2OptionTbl(company.getCode());
			}else {
				resp.setCode(ResponseCode.FAIL);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage(),e);
		}
		
		return resp;
	}

	@Override
	@Transactional
	public int deleteByCodes(List<String> codes) throws Exception{
		try {
			StringBuilder builder = new StringBuilder();
			for(String str: codes) {
				builder.append("'"+str+"'"+",");
			}
			String codesStr = builder.toString();
			codesStr = codesStr.substring(0, codesStr.length()-1);
			List<String> companyIds = mapper.getCompanyIds(codesStr);
			// 批量删除机器码
			pdaMachineCodeMapper.deleteByCompanyIds(companyIds);
			
			int i = mapper.deleteByCodes(codes);
			if(i>0) {
				for(String code:codes) {
					mapper.deleteDbs(code);
				}
			}
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(),e);
		}
		return 1;
	}

	@Override
	public CompanyDetailVo companyDetail(String companyId) {
		return mapper.companyDetail(companyId);
	}

	@Override
	public List<ActivationVo> getActivationList(String companyId) {
		return mapper.getActivationList(companyId);
	}

	@Override
	public void updateByCode(Company company) {
		mapper.updateByCode(company);
		
	}

	@Override
	public void editChannelNum(String companyId, Integer channelNum) throws Exception{
		Company company = new Company();
		company.setId(companyId);
		company.setChannelNum(channelNum);
		mapper.update(company);
		
	}

	

}
